package com.gw.mapper;

import com.gw.pojo.entity.Bucket;
import com.gw.pojo.entity.File;
import com.gw.pojo.vo.FileVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.checkerframework.checker.index.qual.SameLen;

import java.util.List;

@Mapper
public interface FileMapper {

    /**
     * 根据名字查询桶
     * @param bucketName
     * @return
     */
    @Select("select * from buckets where name = #{bucketName}")
    Bucket getBucketByName(String bucketName);

    /**
     *新建桶
     * @param bucketName
     */
    @Insert("insert into buckets (name) values (#{bucketName})")
    void addBucket(String bucketName);

    /**
     * 查询同列表
     * @return
     */
    @Select("select * from buckets order by id asc ")
    List<Bucket> getBuckets();

    /**
     * 删除桶
     * @param bucketName
     */
    @Delete("delete from buckets where name = #{bucketName}")
    void removeBucket(String bucketName);

    /**
     * 插入文件（与桶名关联）
     * @param file
     */
    @Insert("insert into files (file_name, file_url, bucket_name) " +
            "values (#{fileName}, #{fileUrl}, #{bucketName})")
    void addFile(File file);

    /**
     * 查询指定桶所有文件
     * @param bucketName
     * @return
     */
    List<FileVO> getFilesByBucketName(String bucketName);

    /**
     * 通过文件id获取文件下载url
     * @param id
     * @return
     */
    @Select("select file_url from files where id = #{id}")
    String getFileUrlById(Long id);
}
